package com.bussiness.activiti.controller;

import com.activiti.util.ActivitiUtilService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.activiti.dto.ModelNewDto;
import com.bussiness.activiti.service.DeployMentService;
import com.common.response.ResponseBean;
import com.common.response.impl.BaseResponseImpl;
import com.common.util.mypage.Pages;
import com.security.auth.UserDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ProcessDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: zyj
 * @create: 2020-01-15 13:50
 **/
@RestController
@Api(tags={"activiti基础配置相关"})
@RequestMapping("/activitiBase")
public class ActivitiBaseController extends BaseResponseImpl {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    RepositoryService repositoryService;
    @Autowired
    private ActivitiUtilService activitiUtilService;

    @Autowired
    private DeployMentService deployMentService;

    @RequestMapping(value = "/modelList", method = RequestMethod.GET)
    @ApiOperation(value = "模型列表")
    public ResponseBean modelList(Integer pageSize, Integer pageNumber) {
        try {
            List<Model> deployments = repositoryService.createModelQuery().list();
            Pages pages = new Pages(pageNumber, pageSize, deployments);
            return  successData(pages);
        }catch (Exception e){
            e.printStackTrace();
            return error();
        }
    }

    @RequestMapping(value = "/newModel", method = RequestMethod.POST)
    @ApiOperation(value = "创建模型")
    public ResponseBean newModel(@RequestBody ModelNewDto modelNewDto){
        try {
            String id = activitiUtilService.newModel(modelNewDto);
            return successData(id);
        }catch (Exception e){
            logger.error("创建模型失败", e);
            return error();
        }
    }

    @RequestMapping(value = "/deploy", method = RequestMethod.PUT)
    @ApiOperation(value = "部署模型")
    public ResponseBean deploy(String id) {
        try {
            activitiUtilService.deploy(id);
            return success();
        }catch (Exception e){
            logger.error("创建模型失败", e);
            return error();
        }
    }

    @RequestMapping(value = "/getDevolpementForm", method = RequestMethod.GET)
    @ApiOperation(value = "获取部署id获取表单")
    public ResponseBean getDevelopementForm(String developmentId){
        try {
            Map<String, Object> map = activitiUtilService.getDevelopementForm(developmentId);
            return successData(map);
        }catch (Exception e){
            logger.error("获取部署表单失败", e);
            return error();
        }
    }

    @RequestMapping(value = "/getFormByDefId", method = RequestMethod.GET)
    @ApiOperation(value = "获取流程定义id获取表单")
    public ResponseBean getFormByDefId(String defId){
        try {
            Map<String, Object> map = activitiUtilService.getFormByDefId(defId);
            return successData(map);
        }catch (Exception e){
            logger.error("获取部署表单失败", e);
            return error();
        }
    }

    @RequestMapping(value = "/startProcess", method = RequestMethod.PUT)
    @ApiOperation(value = "开启流程")
    public ResponseBean startProcess(){
        try {
            HttpServletRequest httpServletRequest = getRequest();
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            UserDetail userDetail = (UserDetail)  authentication.getPrincipal();
            long userId = userDetail.getId();
            Map<String, Object> formMap = this.getParameterMap(httpServletRequest);

            activitiUtilService.startProcess(formMap, String.valueOf(userId), String.valueOf(formMap.get("processDefId")));
            return success();
        }catch (Exception e){
            logger.error("获取部署表单失败", e);
            return error();
        }
    }

    @RequestMapping(value = "/processList", method = RequestMethod.GET)
    @ApiOperation(value = "部署流程列表")
    public ResponseBean processList(Integer pageSize, Integer pageNumber) {
        try {
            Pages pages = deployMentService.processList(pageSize, pageNumber);
            return  successData(pages);
        }catch (Exception e){
            e.printStackTrace();
            return error();
        }

    }



}
